package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.Appendix;
import com.ruoyi.system.domain.Report;
import com.ruoyi.system.domain.Writings;
import com.ruoyi.system.enums.AuditStateEnum;
import com.ruoyi.system.enums.DocTypeEnum;
import com.ruoyi.system.service.IAppendixService;
import com.ruoyi.system.service.ISysDeptService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Software;
import com.ruoyi.system.service.ISoftwareService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 软件Controller
 * 
 * @author ruoyi
 * @date 2020-04-12
 */
@Controller
@RequestMapping("/system/software")
public class SoftwareController extends BaseController
{
    private String prefix = "system/software";

    @Autowired
    private ISoftwareService softwareService;
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private IAppendixService appendixService;

    @RequiresPermissions("system:software:view")
    @GetMapping()
    public String software()
    {
        return prefix + "/software";
    }

    /**
     * 查询软件列表
     */
    @RequiresPermissions("system:software:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Software software)
    {
        startPage();
        List<Software> list = softwareService.selectSoftwareList(software);
        return getDataTable(list);
    }

    /**
     * 导出软件列表
     */
    @RequiresPermissions("system:software:export")
    @Log(title = "软件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Software software)
    {
        List<Software> list = softwareService.selectSoftwareList(software);
        ExcelUtil<Software> util = new ExcelUtil<Software>(Software.class);
        return util.exportExcel(list, "software");
    }

    /**
     * 新增软件
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存软件
     */
    @RequiresPermissions("system:software:add")
    @Log(title = "软件", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Software software)
    {
        software.setDocType(DocTypeEnum.SOFTWARE.getValue());
        software.setCreator(ShiroUtils.getUserId());
        software.setDepartment(sysDeptService.selectDeptById(software.getDepartmentId()).getDeptName());
        software.setReportUnit(sysDeptService.selectDeptById(software.getReportUnitId()).getDeptName());
        return toAjax(softwareService.insertSoftware(software));
    }

    /**
     * 修改软件
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Software software = softwareService.selectSoftwareById(id);
        mmap.put("software", software);
        mmap.put("dept",sysDeptService.selectDept(software.getReportUnitId()));
        return prefix + "/edit";
    }

    /**
     * 修改保存软件
     */
    @RequiresPermissions("system:software:edit")
    @Log(title = "软件", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Software software)
    {
        software.setDepartment(sysDeptService.selectDeptById(software.getDepartmentId()).getDeptName());
        software.setReportUnit(sysDeptService.selectDeptById(software.getReportUnitId()).getDeptName());
        return toAjax(softwareService.updateSoftware(software));
    }

    /**
     * 删除软件
     */
    @RequiresPermissions("system:software:remove")
    @Log(title = "软件", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(softwareService.deleteSoftwareByIds(ids));
    }



    /**
     * 提交审核
     */
    @RequiresPermissions("system:software:applyAudit")
    @Log(title = "著作", businessType = BusinessType.UPDATE)
    @PostMapping("/applyAudit")
    @ResponseBody
    public AjaxResult applyAudit(long id) {
        Software writings = softwareService.selectSoftwareById(id);
        if (AuditStateEnum.NOT_AUDIT.getValue() != writings.getAuditState().intValue()) {
            return error("请不要重复发起审核");
        } else {
            softwareService.applyAutdit(writings,ShiroUtils.getSysUser());
            return success();
        }

    }

    /**
     * 修改著作
     */
    @GetMapping("/detail/{id}")
    @RequiresPermissions("system:software:detail")
    public String detail(@PathVariable("id") Long id, ModelMap mmap) {
        Software writings = softwareService.selectSoftwareById(id);
        mmap.put("doc", writings);
        List<Appendix> appendices = appendixService.listByDocTypeAndId(writings.getId(), writings.getDocType());
        mmap.put("appendices",appendices);
        return prefix + "/detail";
    }
}
